package com.fanchuan.uzx.ui.fragments;

import java.io.File;
import java.util.concurrent.Executors;

import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.fanchuan.uzx.R;
import com.fanchuan.uzx.ui.utils.ImageGetterAsync;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.ViewById;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.widget.SearchViewCompat;
import android.support.v4.widget.SearchViewCompat.OnQueryTextListenerCompat;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

@EFragment(R.layout.fragments_test_layout)
public class CountingFragment extends SherlockFragment {
	@ViewById
	TextView tvInfo;

	@ViewById
	ImageView ivInfo;
	private ImageGetterAsync mImageGetter;
	AQuery query;

	@AfterViews
	void initData() {

		query = new AQuery(getActivity());
		
		getdata();
	}


	@Background
	void getdata() {
		mImageGetter = new ImageGetterAsync(getActivity(),
				Executors.newSingleThreadExecutor());
		String url = "http://192.168.3.103/server/data.php";
		AjaxCallback.setNetworkLimit(8);
		query.ajax(url, String.class, 24*60*60*1000, new AjaxCallback<String>() {

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				try {
					Log.d("json", object);
					JSONObject jsonObject = new JSONObject(object);
					String topimg = jsonObject.getString("top_img");
					String content = jsonObject.getString("content");
					
					File file = query.getCachedFile(topimg);
					if(file != null && file.exists()){
						ivInfo.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
					}else{
						query.id(R.id.ivInfo).image(topimg);
					}
					tvInfo.setText(Html.fromHtml(content,
							mImageGetter.spawn(content, tvInfo), null));

				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

		});

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setHasOptionsMenu(true);
	}
	
	 @Override
	 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         // Place an action bar item for searching.
         MenuItem item = menu.add("Search");
         item.setIcon(android.R.drawable.ic_menu_search);
         item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
         View searchView = SearchViewCompat.newSearchView(getActivity());
         if (searchView != null) {
             SearchViewCompat.setOnQueryTextListener(searchView,
                     new OnQueryTextListenerCompat() {
                 @Override
                 public boolean onQueryTextChange(String newText) {
                     // Called when the action bar search text has changed.  Update
                     // the search filter, and restart the loader to do a new query
                     // with this filter.
//                     mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
//                     getLoaderManager().restartLoader(0, null, CursorLoaderListFragment.this);
                     return true;
                 }
             });
             item.setActionView(searchView);
         }
     }
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragments_test_layout, null);
		return view;
	}

}
